﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using DIT.Domain;
using System.Globalization;

namespace DIT.Client.MVVM.Converter
{
    public class DateStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            DateTime? dateTime = (DateTime?)value;

            return dateTime.HasValue ? dateTime.Value.ToShortDateString() : null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string dateTimeString = value as string;
            DateTime dateTime;
            if (DateTime.TryParse(dateTimeString, out dateTime))
                return dateTime;
            else
                return null;
        }
    }
}
